package com.itfreer.power;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.utils.HeaderUtils;
import com.itfreer.utils.json.JsonUtils;
import com.itfreer.utils.sys.SystemParameter;

import com.itfreer.power.entity.useredit.UserSysAuthoEntity;

/**
 * 用户权限rest客户端
 */
@Component("PowerRestClient")
public class PowerRestClient {

	private final String checkPasswordUrl = "/power/checkPassword/";
	private final String getUserRolesUrl = "/power/getUserRoles";
	private final String getRoleServiceUrl = "/power/getRoleService";
	private final String addUserServiceUrl = "/power/addUserService";
	private final String removeUserServiceUrl = "/power/removeUserService";
	private final String getRoleMenusUrl = "/power/getRoleMenus";
	private final String getRoleUrlsUrl = "/power/getRoleUrls";
	private final String getRoleDatasUrl = "/power/getRoleDatas";
	private final String getRoleMapLayersUrl = "/power/getRoleMapLayers";
	private final String getRoleMapExtentUrl = "/power/getRoleMapExtent";
	private final String getUserSysAuthoUrl = "/power/getUserSysAuthoEntity";

	@Autowired
	private SystemParameter systemParameter;
	
	/**
	 * 添加用户可访问应用
	 * @param userid
	 * @param service
	 */
	public boolean addUserService(String userid, String service) {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + addUserServiceUrl);
		Response response = target.queryParam("userid", userid)
				.queryParam("service", service)
				.request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null).invoke();
		if (response.getStatus() == 200) {
			String result = response.readEntity(String.class);
			if (result == null) {
				return false;
			} else {
				if (result.trim().toLowerCase().equals("true")) {
					return true;
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	}
	
	/**
	 * 移除用户可访问应用
	 * @param userid
	 * @param service
	 */
	public boolean removeUserService(String userid, String service) {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + removeUserServiceUrl);
		Response response = target.queryParam("userid", userid)
				.queryParam("service", service)
				.request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null).invoke();
		if (response.getStatus() == 200) {
			String result = response.readEntity(String.class);
			if (result == null) {
				return false;
			} else {
				if (result.trim().toLowerCase().equals("true")) {
					return true;
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	}

	/**
	 * 用户密码检查
	 * 
	 * @param userid
	 * @param sPwd
	 * @return
	 */
	public boolean checkPassword(String userid, String sPwd) {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + checkPasswordUrl + userid + "/" + sPwd);
		Response response = target.request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null)
				.invoke();
		if (response.getStatus() == 200) {
			String result = response.readEntity(String.class);
			if (result == null) {
				return false;
			} else {
				if (result.trim().toLowerCase().equals("true")) {
					return true;
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	}

	/**
	 * 获取用户可访问的角色ID集
	 * 
	 * @param userid
	 *            唯一值
	 * @return
	 * @throws IOException
	 */
	public String getUserRoles(String userid) throws IOException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getUserRolesUrl);
		Response response = target.queryParam("uid", userid).request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null).invoke();
		if (response.getStatus() == 200) {
			return response.readEntity(String.class);
		}
		return null;
	}

	/**
	 * 获取用户可访问的应用ID集
	 * 
	 * @param userid
	 *            唯一值
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public List<String> getRoleService(String roles) throws IOException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getRoleServiceUrl);
		Response response = target.queryParam("roles", roles).request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null).invoke();
		if (response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return null;
			}
			List<String> rList = (List<String>) JsonUtils.toBean(resultJson,
					JsonUtils.getCollectionType(List.class, String.class));
			return rList;
		}
		return null;
	}

	/**
	 * 获取用户可访问菜单ID集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public List<String> getRoleMenus(String roles, String serviceId) throws IOException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getRoleMenusUrl);
		Response response = target.queryParam("roles", roles).queryParam("sid", serviceId).request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null)
				.invoke();
		if (response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return null;
			}
			List<String> rList = (List<String>) JsonUtils.toBean(resultJson,
					JsonUtils.getCollectionType(List.class, String.class));
			return rList;
		}
		return null;
	}

	/**
	 * 获取用户可访问url地址集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public List<String> getRoleUrls(String roles, String serviceId) throws IOException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getRoleUrlsUrl);
		Response response = target.queryParam("roles", roles).queryParam("sid", serviceId).request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null)
				.invoke();
		if (response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return null;
			}
			List<String> rList = (List<String>) JsonUtils.toBean(resultJson,
					JsonUtils.getCollectionType(List.class, String.class));
			return rList;
		}
		return null;
	}

	/**
	 * 获取用户数据权限集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public Map<String, String> getRoleDatas(String roles, String serviceId) throws IOException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getRoleDatasUrl);
		Response response = target.queryParam("roles", roles).queryParam("sid", serviceId).request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null)
				.invoke();
		if (response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return null;
			}
			return (Map<String, String>) JsonUtils.toBean(resultJson, Map.class);
		}
		return null;
	}

	/**
	 * 获取用户可访问的地图图层集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public Map<String, String> getRoleMapLayers(String roles, String serviceId) throws IOException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getRoleMapLayersUrl);
		Response response = target.queryParam("roles", roles).queryParam("sid", serviceId).request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null)
				.invoke();
		if (response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return null;
			}
			return (Map<String, String>) JsonUtils.toBean(resultJson, Map.class);
		}
		return null;
	}

	/**
	 * 获取用户可访问的地图范围集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public List<String> getRoleMapExtent(String roles, String serviceId) throws IOException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getRoleMapExtentUrl);
		Response response = target.queryParam("roles", roles).queryParam("sid", serviceId).request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null)
				.invoke();
		if (response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return null;
			}
			List<String> rList = (List<String>) JsonUtils.toBean(resultJson,
					JsonUtils.getCollectionType(List.class, String.class));
			return rList;
		}
		return null;
	}
	
	/**
	 * 通过系统id访问用户申请信息（申请/取消申请）
	 * @param serviceId
	 * @return
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public List<UserSysAuthoEntity> getUserSySAuthoEntity(String serviceId) throws IOException{
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getUserSysAuthoUrl);
		Response response = target.queryParam("sysId", serviceId).request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null)
				.invoke();
		if(response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return null;
			}
			
			List<UserSysAuthoEntity> rList = (List<UserSysAuthoEntity>) JsonUtils.toBean(resultJson,
					JsonUtils.getCollectionType(List.class, String.class));
			return rList;
		}
		return null;
	};
}